new Vue({
    el:'#newsList',
    data:{
        pageInfo:'',
        title:'',
        createTime:'',
        styleObj:{
            backgroundColor:"#ff4e00",
            border:"none",
            height:"23px",
            width:"23px",
            color:"white"
        },
        infoFlag:false,
        infoText:'',
        no_bgc:{
            height:"22px",
            width:"22px",
        },
        title1:'',
        content:'',
        addFlag:false,
        updateFlag:false
    },
    methods:{
        getPageInfo(pageNum,pageSize)
        {
            axios({
                url:"/api/getNewsPage",
                params:
                {
                    pageNum,
                    pageSize,
                    title:this.title,
                    createTime:this.createTime
                }}
            ).then(rs=>{
                console.log(rs.data)
                this.pageInfo=rs.data.pageInfo
            })
        },
        opera(sign)
        {
            let pageNum=1
            switch(sign)
            {
                case "pre":
                    pageNum=this.pageInfo.hasPreviousPage?this.pageInfo.prePage:1
                    break;
                case "next":
                    pageNum=this.pageInfo.hasNextPage?this.pageInfo.nextPage:this.pageInfo.pages
                    break;
                case "last":
                    pageNum=this.pageInfo.pages
                    break;
            }
            this.getPageInfo(pageNum,10)
        },
        //删除咨询
        deleteNews(id)
        {
            if(confirm("确认删除该条咨询吗？"))
            {
                axios.get(
                    '/api/deleteNewsById',{
                        params:{
                            id
                        }
                    }

                ).then(rs=>{
                    if(rs.data.delMsg==="yes")
                    {
                        this.infoFlag=true
                        this.infoText="删除成功"
                    }else{
                        this.infoFlag=true
                        this.infoText="删除失败"
                    }
                })
            }

        },
        update(id)
        {
            window.location=`/api/NewsUpdate.html?id=${id}`
        },
        NewsAdd()
        {
            if(this.title1==='')
            {
                this.$refs.span_title1.innerHTML="标题不能为空"
                this.$refs.span_title1.style.color="red"
                this.$refs.span_title1.style.fontSize="12px"
                return false
            }
            if(this.content==='')
            {
                this.$refs.span_content.innerHTML="内容不能为空"
                this.$refs.span_content.style.color="red"
                this.$refs.span_content.style.fontSize="12px"
                return false
            }
            if(confirm("确认保存吗？"))
            {
                axios({
                    url:'/api/newsAdd',
                    params:{
                        title:this.title1,
                        content:this.content
                    }
                }).then(rs=>{
                    console.log(rs.data)
                    if(rs.data.addMsg==="success")
                    {
                        this.infoFlag=true
                        this.infoText="添加成功"
                        this.addFlag=false
                    }else{
                        this.infoFlag=true
                        this.infoText="添加失败"
                    }
                }).catch(error=>{
                    console.log(error)
                    alert(error)
                })
            }
        },
        checkTitle()
        {
            axios.get('/api/checkTitle',
                {
                    params:{
                        title:this.title1
                    }
                }).then(rs=>{
                console.log(this.$refs.span_title1)
                if(rs.data.isCheck==="yes"||this.title1==='')
                {
                    this.$refs.add.disabled=true
                    this.$refs.span_title1.innerHTML="标题已存在"
                    this.$refs.span_title1.style.color="red"
                    this.$refs.span_title1.style.fontSize="12px"
                }else{
                    this.$refs.add.disabled=false
                    this.$refs.span_title1.innerHTML="标题可用"
                    this.$refs.span_title1.style.color="green"
                    this.$refs.span_title1.style.fontSize="12px"
                }
            })
        },
        closeInfo()
        {
            this.infoFlag=false
            window.location="/api/newsList.html"
        }
    },
    created(){
        this.getPageInfo(1,10)
    }
})